package com.tsd.system.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.CommonMsgNameConstants;
import com.tsd.CommonParamNameConstants;
import com.tsd.common.controller.BaseController;
import com.tsd.core.constants.Constants;
import com.tsd.core.datasource.DataSourceContextHolder;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.entity.SysBugWorksheet;
import com.tsd.system.entity.SysBugWorksheetExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysAccountSetService;
import com.tsd.system.service.SysBugWorksheetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysBugWorksheet系统问题工单 控制层
 *
 * @author david
 * @date 2022/09/27 17:58:22
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "sys_bug_worksheet-系统问题工单", description = "系统问题工单的API描述")
public class SysBugWorksheetController extends BaseController {

    @Resource
    private SysBugWorksheetService sysBugWorksheetService;
    @Resource
    private SysAccountSetService sysAccountSetService;

    @ApiOperation(value = "根据sid获取系统问题工单", notes = "根据sid获取系统问题工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "系统问题工单sid", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getSysBugWorksheetBySid.action", method = RequestMethod.POST)
    public HttpResult<Object> getSysBugWorksheetBySid() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String sid = HlpUtils.getString(map, CommonParamNameConstants.SID);
            super.checkEmpty(sid, CommonMsgNameConstants.EMPTY_MSG_SID);
            // 设置为主库
            DataSourceContextHolder.setDataSourceType(DataSourceContextHolder.DEFAULT_ACCOUNT_SET_SID);
            SysBugWorksheet sysBugWorksheet = sysBugWorksheetService.queryBySid(sid);
            hr.setData(sysBugWorksheet);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除系统问题工单", notes = "根据id删除系统问题工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sids", value = "系统问题工单sids", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteSysBugWorksheets.action", method = RequestMethod.POST)
    public HttpResult<Object> deleteSysBugWorksheets() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object sids = map.get(CommonParamNameConstants.SIDS);
            super.checkEmpty(sids, CommonMsgNameConstants.EMPTY_MSG_SIDS);
            List<String> list = JSON.parseArray(JSON.toJSONString(sids), String.class);
            // 设置为主库
            DataSourceContextHolder.setDataSourceType(DataSourceContextHolder.DEFAULT_ACCOUNT_SET_SID);
            sysBugWorksheetService.deleteSysBugWorksheets(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存系统问题工单", notes = "保存系统问题工单，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "系统问题工单", required = true, paramType = "body", dataType = "SysBugWorksheet")
    })
    @ResponseBody
    @RequestMapping(value = "/saveSysBugWorksheet.action", method = RequestMethod.POST)
    public HttpResult<Object> saveSysBugWorksheet() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String json = JSON.toJSONString(map);
            SysBugWorksheet t = JSON.parseObject(json, SysBugWorksheet.class);
            String account_set_sid = DataSourceContextHolder.getDataSourceType();
            t.setAccount_sid(account_set_sid);
            // 设置为主库
            DataSourceContextHolder.setDataSourceType(DataSourceContextHolder.DEFAULT_ACCOUNT_SET_SID);
            sysBugWorksheetService.saveSysBugWorksheet(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询系统问题工单", notes = "分页查询系统问题工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findSysBugWorksheets.action", method = RequestMethod.POST)
    public HttpResult<Object> findSysBugWorksheets() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            DataSourceContextHolder.setDataSourceType(DataSourceContextHolder.DEFAULT_ACCOUNT_SET_SID);
            Map<String, Object> params = new HashMap<>();
            String parent_sid = HlpUtils.getString(map, CommonParamNameConstants.PARENT_SID, Constants.ROOT_PARENT_SID);
            params.put(CommonParamNameConstants.PARENT_SID, parent_sid);
            HlpUtils.copyIntegerValue(map, params, CommonParamNameConstants.DR, false, 0);
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.SEARCH_VALUE, false, true);
            PageBean page = new PageBean(map);
            List<SysBugWorksheetExt> list = sysBugWorksheetService.findSysBugWorksheets(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "获取系统问题工单处理记录", notes = "获取系统问题工单处理记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parent_sid", value = "问题记录sid", required = false, dataType = "string", paramType = "form"),
    })
    @ResponseBody
    @RequestMapping(value = "/findSysBugWorksheetsByParentSid.action", method = RequestMethod.POST)
    public HttpResult<Object> findSysBugWorksheetsByParentSid() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            DataSourceContextHolder.setDataSourceType(DataSourceContextHolder.DEFAULT_ACCOUNT_SET_SID);
            String parent_sid = HlpUtils.getString(map, CommonParamNameConstants.PARENT_SID);
            super.checkEmpty(parent_sid, CommonMsgNameConstants.getEmptyMsg2(CommonParamNameConstants.PARENT_SID));
            List<SysBugWorksheetExt> list = sysBugWorksheetService.findSysBugWorksheetsByParentSid(parent_sid);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "处理系统问题工单", notes = "处理系统问题工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "系统问题工单", required = true, paramType = "body", dataType = "SysBugWorksheet")
    })
    @ResponseBody
    @RequestMapping(value = "/handleSysBugWorksheet.action", method = RequestMethod.POST)
    public HttpResult<Object> handleSysBugWorksheet() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String json = JSON.toJSONString(map);
            SysBugWorksheet worksheet = JSON.parseObject(json, SysBugWorksheet.class);
            String account_set_sid = DataSourceContextHolder.getDataSourceType();
            worksheet.setAccount_sid(account_set_sid);
            // 设置为主库
            DataSourceContextHolder.setDataSourceType(DataSourceContextHolder.DEFAULT_ACCOUNT_SET_SID);
            sysBugWorksheetService.handleSysBugWorksheet(worksheet, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
